package com.glsc.ngateway.common.base.repo.mysql.gateway.oaflow;

import com.glsc.ngateway.common.base.domain.mysql.gateway.oaflow.xirtrade.TOaFlowXirTradeConfig;
import com.glsc.ngateway.common.api.oaflow.dto.xirtrade.TOaFlowXirTradeConfigSearchParamDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 衡泰指令同步oa流程配置
 */
@Repository
public interface TOaFlowXirTradeConfigRepository extends JpaRepository<TOaFlowXirTradeConfig, Long> {

    @Query(value = "select * from t_oa_flow_xir_trade_config t " +
            "where t.deleted=0 " +
            "and if(:#{#param.date} is not null, " +
            "       ((t.begin_time is null or t.begin_time <= :#{#param.date}) " +
            "   and (t.end_time is null or t.end_time >= :#{#param.date})) " +
            ", 1=1 ) " +
            "and (coalesce(:#{#param.workflowTypeList}, null) is null or t.workflow_type in (:#{#param.workflowTypeList})) " +
            "", nativeQuery = true)
    Page<TOaFlowXirTradeConfig> page(Pageable pageable, @Param("param") TOaFlowXirTradeConfigSearchParamDto param);

    @Query(value = "select * from t_oa_flow_xir_trade_config t " +
            "where t.deleted=0 " +
            "and if(:#{#param.date} is not null, " +
            "       ((t.begin_time is null or t.begin_time <= :#{#param.date}) " +
            "   and (t.end_time is null or t.end_time >= :#{#param.date})) " +
            ", 1=1 ) " +
            "and (coalesce(:#{#param.workflowTypeList}, null) is null or t.workflow_type in (:#{#param.workflowTypeList})) " +
            "", nativeQuery = true)
    List<TOaFlowXirTradeConfig> queryAll(@NotNull @Param("param") TOaFlowXirTradeConfigSearchParamDto param);

    List<TOaFlowXirTradeConfig> findAllByWorkflowIdIn(List<String> workflowId);
}
